package com.terios.logic.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.terios.R;
import com.terios.Status;
import com.terios.logic.ui.imaCache.Anseylodar;

/**
 * WeiboInfo.java 2012-10-15 下午11:40:14
 * 
 * @author Wang Tengrui
 */
public class WeiboInfo extends Activity implements Runnable {

	ImageView weiboinfo_profile_preview;// 发微博人的头像
	TextView weiboinfo_profile_name;// 发微博的人
	TextView weiboinfo_message;// 微博内容
	ImageView weiboinfo_pic;//原微博配图
	TextView weiboinfo_oriTxt;// 转发内容
	ImageView weiboinfo_sub_pic;// 转发内容图片
	ImageView weiboinfo_profile_vip;//认证图标
	public Status status;// 返回的微博内容
	public Status retweeted_status;// 转发内容

	LinearLayout retweeted_status_view;// 转发微博内容布局
	ImageView back;// 返回
	View progress;// 滚动的菊花
	//Button comment, redirect;// 评论和转发按钮
	TextView comment_num, redirect_num;// 转发和评论
	TextView tvtitle;
	Anseylodar anseylodar;// 异步加载器
	RelativeLayout weiboinfo_profile;
	// 刷新 评论 转发 收藏 更多
	TextView tvReload, tvComment, tvForward, tvFav, tvMore;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			refreshview();// 刷新页面内容
			// 如果使用又转发 则显示又转发页面
			if (retweeted_status != null) {
				weiboinfo_pic.setVisibility(View.GONE);
				retweeted_status_view.setVisibility(View.VISIBLE);
				refeshtweestatus();
			}
		}
	};

	@SuppressWarnings("static-access")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(getWindow().FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_weiboinfo);
		initview();// 初始化 页面
		retweeted_status_view = (LinearLayout) this.findViewById(R.id.src_text_block);
		anseylodar = new Anseylodar();
		Thread statusT = new Thread(this);
		statusT.start();
	}

	public void initview() {
		try {
			progress = this.findViewById(R.id.weiboinfo_progress);
			View title = this.findViewById(R.id.weiboinfo_title);
			weiboinfo_profile_preview = (ImageView) this.findViewById(R.id.weiboinfo_profile_preview);
			weiboinfo_profile_name = (TextView) this.findViewById(R.id.weiboinfo_profile_name);
			weiboinfo_message = (TextView) this.findViewById(R.id.weiboinfo_message);
			weiboinfo_pic = (ImageView) this.findViewById(R.id.weiboinfo_pic);
			weiboinfo_oriTxt = (TextView) this.findViewById(R.id.weiboinfo_oriTxt);
			weiboinfo_sub_pic = (ImageView) this.findViewById(R.id.weiboinfo_sub_pic);
			tvtitle = (TextView) title.findViewById(R.id.tvinfo);
			tvtitle.setText(R.string.weiboinfo);
			back = (ImageView) title.findViewById(R.id.title_bt_left);
			back.setImageResource(R.drawable.title_back);
			Button home = (Button) title.findViewById(R.id.title_bt_right);
			home.setVisibility(View.GONE);
			tvReload = (TextView) this.findViewById(R.id.tvReload);
			tvComment = (TextView) this.findViewById(R.id.tvComment);
			tvForward = (TextView) this.findViewById(R.id.tvForward);
			tvFav = (TextView) this.findViewById(R.id.tvFav);
			tvMore = (TextView) this.findViewById(R.id.tvMore);
			weiboinfo_profile = (RelativeLayout) this.findViewById(R.id.weiboinfo_profile);
			comment_num = (TextView) this.findViewById(R.id.comment_num);
			redirect_num= (TextView) this.findViewById(R.id.redirect_num);
			// 底部菜单点击事件
			tvReload.setOnClickListener(new bottomBtnOnClick());
			tvComment.setOnClickListener(new bottomBtnOnClick());
			tvForward.setOnClickListener(new bottomBtnOnClick());
			tvFav.setOnClickListener(new bottomBtnOnClick());
			tvMore.setOnClickListener(new bottomBtnOnClick());


		} catch (Exception e) {
			e.printStackTrace();
		}
		// 返回
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				WeiboInfo.this.finish();
			}
		});

		// 点击跳转到用户信息页面
		weiboinfo_profile.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String userid = String.valueOf(status.getUser().getId());
				Intent intent = new Intent(WeiboInfo.this, UserInfoActivity.class);
				intent.putExtra("userid", userid);
				WeiboInfo.this.startActivity(intent);
			}
		});
	}

	// 跳转到评论页面(单条评论)
	private void goComment() {
		/*
		 * long statusid = status.getId(); Intent intent=new
		 * Intent(WeiboInfo.this, AddComment.class); intent.putExtra("statusid",
		 * statusid); WeiboInfo.this.startActivity(intent);
		 */
	}

	// 跳转到转发页面
	private void goRediret() {
		/*
		 * Intent intent=new Intent(WeiboInfo.this,Respostweibo.class); //当前微博ID
		 * intent.putExtra("sid",String.valueOf(status.getId()));
		 * intent.putExtra("status",
		 * "@"+status.getUser().getScreenName()+" "+status
		 * .getText().toString()); intent.putExtra("user",
		 * status.getUser().getName().toString());
		 * WeiboInfo.this.startActivity(intent);
		 */
	}

	// 底部按钮的点击事件
	public class bottomBtnOnClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tvReload:// 刷新
				break;
			case R.id.tvComment:// 评论
				goComment();
				break;
			case R.id.tvForward:// 转发
				goRediret();
				break;
			case R.id.tvFav:// 收藏
				// WeiboUtil.addFav(WeiboInfo.this, status.getId());
				// Toast.makeText(WeiboInfo.this, "收藏成功",
				// Toast.LENGTH_SHORT).show();
				break;
			case R.id.tvMore:// 更多
				break;
			}
		}
	}

	public void refreshview() {

		try { // 发表微博人的名字
			weiboinfo_profile_name.setText(status.getUser().getScreenName());
			// 微博内容
			weiboinfo_message.setText(status.getText().toString());
			progress.setVisibility(View.GONE);
			if (status.getUser().isVerified()) {//判断是否为认证用户
				weiboinfo_profile_vip = (ImageView) this.findViewById(R.id.weiboinfo_profile_vip);
				weiboinfo_profile_vip.setVisibility(View.VISIBLE);// 显示认证图标
			} else {
				weiboinfo_profile_vip = (ImageView) this.findViewById(R.id.weiboinfo_profile_vip);
				weiboinfo_profile_vip.setVisibility(View.GONE);//隐藏认证图标
			}
			String url = status.getUser().getProfileImageUrl();
			anseylodar.showimgAnsy(weiboinfo_profile_preview, url);// 加载头像
			if (!TextUtils.isEmpty(status.getThumbnail_pic())) { // 加载原微博配图
				retweeted_status_view.setVisibility(View.GONE);
				weiboinfo_pic.setVisibility(View.VISIBLE);
				anseylodar.showimgAnsy(weiboinfo_pic, status.getThumbnail_pic());
			}
			
			comment_num.setText(String.valueOf(status.getReposts_count()));
			redirect_num.setText(String.valueOf(status.getComments_count()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 转发内容
	public void refeshtweestatus() {
		// 获取转发内容和图片
		try {
			// 转发内容
			weiboinfo_oriTxt.setText(retweeted_status.getText().toString());
			// 加载转发微博配图
			if(!TextUtils.isEmpty(retweeted_status.getThumbnail_pic())){
				weiboinfo_sub_pic.setVisibility(View.VISIBLE);
				anseylodar.showimgAnsy(weiboinfo_sub_pic, retweeted_status.getThumbnail_pic());
			}else {
				weiboinfo_sub_pic.setVisibility(View.GONE);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		// 获取HomeActivity传送的数据
		Intent data = getIntent();
		status = (Status) data.getExtras().getSerializable("status");
		// 获取转发内容
		retweeted_status = status.getRetweeted_status();
		mHandler.sendEmptyMessage(0);
	}

}
